package com.py.androidUtils.UI.view;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.py.androidUtils.utils.FileManagerUtils;
import com.readyidu.app.R;

/**
 * @说明 拍照选择图片
 * @作者 LY
 * @文件 ShowPhotosView.java
 * @时间 2015年7月21日 上午10:54:39
 */
public class ShowPhotosView {
	public static final int IMAGES_ACTION_CAMERA = 100;// 拍照
	public static final int IMAGES_ACTION_SELECT_IMAGES = 101;// 选取相册
	private String imagesPath = "";

	private Context mContext;

	public ShowPhotosView(Context context) {
		this.mContext = context;
	}

	public void showView(View view) {
		View photosiew = LayoutInflater.from(mContext).inflate(R.layout.common_show_photos, null);
		final PopupWindow popupWindow = new PopupWindow(photosiew, android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
		popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
		((TextView) photosiew.findViewById(R.id.btnPhotoAlbum)).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				selectImages(IMAGES_ACTION_SELECT_IMAGES);
				photosClickListener.selectImages(IMAGES_ACTION_SELECT_IMAGES);
			}
		});
		((TextView) photosiew.findViewById(R.id.btnCamera)).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				letCamera(IMAGES_ACTION_CAMERA);
				photosClickListener.letCamera(imagesPath, IMAGES_ACTION_CAMERA);
			}
		});
		((TextView) photosiew.findViewById(R.id.btnCancel)).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
			}
		});
		((LinearLayout) photosiew.findViewById(R.id.line_bg)).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
			}
		});
	}

	protected void letCamera(int requestCode) {
		Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		String strImgPath = FileManagerUtils.getInstance().getAppPath() + "/img/";
		String fileName = System.currentTimeMillis() + ".jpg";// 照片命名
		File out = new File(strImgPath);
		if (!out.exists()) {
			out.mkdirs();
		}
		out = new File(strImgPath, fileName);
		strImgPath = strImgPath + fileName;// 该照片的绝对路径
		imagesPath = strImgPath;
		Uri uri = Uri.fromFile(out);
		imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		((Activity) mContext).startActivityForResult(imageCaptureIntent, requestCode);
	}

	protected void selectImages(int requestCode) {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		intent.setType("image/*");
		((Activity) mContext).startActivityForResult(intent, requestCode);
	}

	public interface PhotosClickListener {
		void letCamera(String imagesPath, int requestCode);

		void selectImages(int requestCode);
	}

	public PhotosClickListener photosClickListener;

	public void setPhotosClickListener(PhotosClickListener photosClickListener) {
		this.photosClickListener = photosClickListener;
	}

}
